//---------------------------------------------------------------------
// File: ApplicationStatusStep.cs
// 
// Summary: 
using System;
using System.Xml;
using BizUnit.BizUnitOM;
using Microsoft.BizTalk.ExplorerOM;

namespace BizUnit.Extensions.BizTalk
{
    using Helpers;

    /// <remarks>
    /// This step gets the status of the specified application and loads it into the context 
    /// into a key named "ApplicationStatus"
    /// 
    /// <code escaped="true">
    /// <TestStep assemblyPath="BizUnit.Extensions.dll" typeName="BizUnit.Extensions.ApplicationGetStatusStep">
    ///		<ApplicationName></ApplicationName>
    ///		<ConnectionString></ConnectionString>
    /// </TestStep>
    /// </code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>ApplicationName</term>
    ///			<description>The name of the application for which we need to get the status</description>
    ///		</item>
    /// 	<item>
    ///			<term>ConnectionString</term>
    ///			<description>Optional. The connectionstring to the management database. If not provided, then a local connection will be assumed</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class ApplicationGetStatusStep : ITestStep, ITestStepOM
    {
        private BtsCatalogExplorer root;
        private IBizTalkApplication appInstance;
        private string applicationName;
        private string connectionString;

        private Status status;

        public string ApplicationName
        {
            get
            {
                return applicationName;
            }
            set
            {
                applicationName = value;
            }
        }
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
            set
            {
                connectionString = value;
            }
        }
        public Status Status
        {
            get
            {
                return status;
            }

        }
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ApplicationName = context.ReadConfigAsString(testConfig, "ApplicationName");
            ConnectionString = context.ReadConfigAsString(testConfig, "ConnectionString", true);
            XmlNode validationConfig = testConfig.SelectSingleNode("ValidationStep");

            Execute(context);

            // if there is a validation step it will be executed, else control will just be returned
            context.ExecuteValidator(null, validationConfig);


        }



        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);

            appInstance = BizTalkStepHelper.GetApplication(applicationName, connectionString);

            //add the current application name to the context if it doesnt already exist

            if (String.IsNullOrEmpty(context.GetValue("CurrentApplication")))
            {
                context.Add("CurrentApplication", appInstance.Name);
            }
            //if the status is not null add it to the context, else add a blank string to the context
            if (appInstance != null)
            {
                context.Add("ApplicationStatus", appInstance.Status.ToString());
            }
            else
            {
                context.LogError(String.Format("could not get the application status for application {0}", applicationName));
                context.Add("ApplicationStatus", "");
            }
        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ApplicationName, "ApplicationName");


        }

        #endregion
    }
}
